package com.freewater.javabase.demo.generic.extend;

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        Perform p = new Perform();
        List<Dog> dogs = new ArrayList<>();
        List<Cat> cats = new ArrayList<>();
        dogs.add(new Dog());
        cats.add(new Cat());

        p.perform(dogs);
        p.perform(cats);
        p.doSomething(dogs);
    }
}

abstract class Animal {
    public abstract void sing();
}

class Cat extends Animal {
    @Override
    public void sing() {
        System.out.println("Hello, audience, I'm a Cat.");
    }
}

class Dog extends Animal {
    @Override
    public void sing() {
        System.out.println("Hello, audience, I'm a dog.");
    }
}

class AmericaDog extends Dog {
    @Override
    public void sing() {
        System.out.println("Hello, audience, I'm a America dog.");
    }
}

class Perform {

    /**
     * 上边界演示
     *
     * @param animals
     */
    public void perform(List<? extends Animal> animals) {
        for (Animal animal : animals) {
            animal.sing();
        }
        // animals的add方法不能调用，甚至参数为new Object()也不行
        /*
        animals.add(new Animal() {
            @Override
            public void sing() {
                System.out.println("");
            }
        });
        animals.add(new Dog());
        animals.add(new Object());
        */
    }

    /**
     * 下边界演示
     *
     * @param animals
     */
    public void doSomething(List<? super AmericaDog> animals) {
        for (Object animal : animals) {
            System.out.println(animal);
        }
        //animals.add(new Object()); // 不可以
        //animals.add(new Dog()); // 不可以
        animals.add(new AmericaDog()); // 可以
    }
}
